# -*- coding: utf-8 -*-
"""
Created on Wed Jun 28 15:04:37 2017

@author: qiang.qian
"""
import json
import requests
import datetime
import sys

stamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")

file_index = "01"

result_file = open("D:\\QIAN\\CODE\\oschina\\userprofile\\profiles_"+file_index + "_"+ stamp + ".txt", "w")

text_file = open("D:\\QIAN\\CODE\\oschina\\userprofile\\uids_"+ file_index + ".txt", "r")

print(datetime.datetime.now().strftime("%Y%m%d_%H%M%S"))


def callapi(uid):
    try:
        values = {
        	"Parameters":[]
        }
       
        values['UID'] = uid
        
        url = 'http://ws.members.ctripcorp.com/userprofileservice/api/GetAllUserProfile'
        data = json.dumps(values)
        headers = {'content-type': 'application/json'}
        res = requests.post(url=url,data=data,headers=headers)
        
        j = json.loads(res.text)
        
        return j['ProfileList']
    except KeyboardInterrupt as e:
        raise e
    except:
        print(uid)
        print("Unexpected error:", sys.exc_info()[0])
        return []
    
def saveprofiles(uid,profiles):
    try:
        for profile in profiles:
            result_file.write("{0}\t{1}\t{2}\n".format(uid,profile['Key'],profile['Value']))
        return 
    except KeyboardInterrupt as e:
        raise e
    except:
        print(uid)
        print("Unexpected error:", sys.exc_info()[0])
        return []

lines = [line.rstrip('\n') for line in text_file]
i=0
try:
    for uid in lines:
        i = i+ 1
        print(i)
        profiles=callapi(uid)
        saveprofiles(uid,profiles) 
        if(i % 10000 == 0):
            result_file.flush()
    print(datetime.datetime.now().strftime("%Y%m%d_%H%M%S"))
    print('done')
except KeyboardInterrupt:
    print("break")
    sys.exit()
finally:
    result_file.flush()
    result_file.close()
    text_file.close()

    